package com.xc.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.demo.entity.Notice;
import com.xc.demo.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/notice")
public class NoticeController {


    @Resource
    private NoticeService noticeService;


    @PutMapping
    public Notice update(@RequestBody Notice notice){

        noticeService.updateById(notice);

        return notice;
    }



    @PostMapping
    public Boolean saveNotices(@RequestBody Notice notice){

         return   noticeService.saveOrUpdate(notice);

    }


    @GetMapping("/page")
    public IPage<Notice> page(@RequestParam Integer pageNum,@RequestParam Integer pageSize
            ,@RequestParam(defaultValue = "")  String uname){

        IPage<Notice>  page = new Page<>(pageNum,pageSize);

        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();


        if(!"".equals(uname)){
            queryWrapper.eq("title",uname);
        }

        queryWrapper.orderByAsc("id");


        return noticeService.page(page,queryWrapper);

    }



}
